package com.wu.controller;

import com.wu.feign.UserFeign;
import com.wu.pojo.User;
import com.wu.vo.UserVo;
import org.omg.CORBA.StringHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserFeign userFeign;

    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    @GetMapping("/findall")
    @ResponseBody
    public UserVo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int index = (page-1)*limit;
        return new UserVo().setCode(0).setMsg("").setCount(userFeign.count()).setData(userFeign.findAll(index, limit));
    }

    @GetMapping("/count")
    public int count(){
        return userFeign.count();
    }

    @PostMapping("/save")
    public String save(User user){
        user.setRegisterdate(new Date());
        userFeign.save(user);
        return "redirect:/user/redirect/user_manage";
    }

    //这个是为了让我们直接在manage页面点击添加的 不走后端了就
    @GetMapping("/add")
    public String add(){
        return "redirect:/user/redirect/user_add";
    }

    @GetMapping("/deleteById/{id}")
    public String delete(@PathVariable("id") long id){
        userFeign.delete(id);
        return "redirect:/user/redirect/user_manage";
    }

}
